from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from goods.models import SKU, GoodsCategory, GoodsChannel

# 面包屑导航
from goods.search_indexes import SKUIndex


class CategorySerializer(serializers.ModelSerializer):
    """类别序列化器 """
    class Meta:
        model = GoodsCategory
        fields = ('id', 'name')

class ChannelSerializer(serializers.ModelSerializer):
    """频道序列化器 """
    category = CategorySerializer()

    class Meta:
        model = GoodsChannel
        fields = ('category', 'url')




class SKUSerializer(ModelSerializer):
    """序列化器序输出商品列表SKU信息"""
    class Meta:
        model = SKU
        # 输出：序列化的字段
        fields = ('id', 'name', 'price', 'default_image_url', 'comments')


class SKUIndexSerializer(HaystackSerializer):
    """
    SKU索引结果数据序列化器
    """
    class Meta:
        index_classes = [SKUIndex] # 指定关联的索引模型类
        fields = ['text', 'id', 'name', 'price', 'default_image_url', 'comments'] # 序列化索引对象成字典


